package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据分类id查询套餐集合 只查询在售状态下的套餐 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<SetmealVO> getByCategoryIdList(Setmeal setmeal) {
      List<SetmealVO> setmealList = setmealMapper.seleteByCategoryId(setmeal);
      log.info("根据分类id查询到的套餐数据为:{}",setmealList);
        return setmealList;
    }

    /**
     * 根据套餐id查询所包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getBySetmealIdDish(Integer id) {
      List<DishItemVO> dishItemVOList = setmealMapper.seleteByIdDish(id);
        return dishItemVOList;
    }

    /**
     * 分页查询 及条件查询套餐
     * @return
     */
    @Override
    public PageResult getPageList(SetmealPageQueryDTO setmealPageQueryDTO) {
     PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
     Page<SetmealVO> page = setmealMapper.seletePage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 保存套餐
     * @param dto
     */
    @Transactional
    @Override
    public void save(SetmealDTO dto) {
        //先保存到套餐表中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.saveSetmeal(setmeal);
        //再保存到套餐菜品表中
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() >0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            log.info("赋完setmealid值之后的菜品集合id为:{}",setmealDishes);
            setmealMapper.saveDishs(setmealDishes);
        }
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //先查询状态 起售的套餐不能删除
      List<Setmeal> setmealList = setmealMapper.selectByIds(ids);
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //再删除套餐表
        setmealMapper.deleteByIds(ids);
        //最后删除套餐及商品表
        setmealMapper.deleteSetmealDishByIds(ids);

    }

    /**
     * 根据套餐id查找套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        //先查找套餐表
        Setmeal setmeal = setmealMapper.selectById(id);
      //再查找套餐包含的菜品表集合
      List<SetmealDish> setmealDish = setmealMapper.selectDishBySetmealId(id);
       SetmealVO setmealVO = new SetmealVO();
       BeanUtils.copyProperties(setmeal,setmealVO);
       setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    /**
     * 修改套餐属性
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表的属性
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateStemeal(setmeal);
        //修改套餐菜品表属性  先删除套餐包含菜品
        setmealMapper.deleteSetmealDishById(setmealDTO.getId());
        //再添加口味表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null){
            //因为前端传过来的菜品集合中套餐id为空 所以要赋值
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealMapper.saveDishs(setmealDTO.getSetmealDishes());
        }
    }

    /**
     * 修改套餐状态
     * @param setmeal
     */
    @Override
    public void setStatus(Setmeal setmeal) {
        //查找菜品表 看套餐所包含的菜品是否都为在售状态
        List<SetmealDish> setmealDishes = setmealMapper.selectDishBySetmealId(setmeal.getId());
        List<Long> dishIds = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            dishIds.add(dishId);
        }
        log.info("得到的dishid集合为:{}",dishIds);
        List<Dish> dishList = dishMapper.seleteById(dishIds);
        for (Dish dish : dishList) {
          if (  dish.getStatus() == StatusConstant.DISABLE){
              throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
          }
        }
        setmealMapper.updateStemeal(setmeal);
    }
}
